// pages/dateTime/dateTime.js
var wxRequest = require('../../utils/request.js');
var wxCharts = require('../../utils/wxcharts.js');
var pieChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // color: [
    //   {
    //     month: 'current',//要标记的日期所属月份，有效值有prev（上个月）,current（当前月），next（下个月）
    //     day: "01",//要标记的日期
    //     color: 'white',//日期文字的颜色，格式为“#HEX”或CSS颜色名
    //     background: "pink",//日期单元格的颜色，格式为“#HEX”或CSS颜色名
    //   }, {
    //     month: 'current',
    //     day: "05",
    //     color: 'white',
    //     background: "black",
    //   },
    // ],
    color : [],

    PageCur: '',
    dakaArr:[],
    user: [],
    month:0,
    lastTime : '',
    startTime: '',
    pieChartData:[],
    total: '',
  },

  NavChange(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.cur,
    })
  },

  /**
   * 点击上个月
   */
  next: function (e) {
    // console.log(event.detail.currentMonth);
    var data = {
      yearMonth: e.detail.currentYear + '-' + e.detail.currentMonth,
    };
    var monthUrl = '/api/wx/getMonthData?wxToken=' + wx.getStorageSync('openid');
    wxRequest.postRequest(monthUrl, data, this.monthUrlSuccess);
  },

  monthUrlSuccess:function(res){
    this.setData({
      color: res.data.monthList,
    })
  },

  /**
   * 点击上个月
   */
  prev: function (e) {
    // console.log(e)
    // const currentYear = e.detail.currentYear;
    // const currentMonth = e.detail.currentMonth;
    // wx.showModal({
    //   title: '当前日期',
    //   content: '当前年份：' + currentYear + '年\n当前月份：' + currentMonth + '月'
    // });

    var data = {
      yearMonth: e.detail.currentYear + '-' + e.detail.currentMonth,
    };
    var monthUrl = '/api/wx/getMonthData?wxToken=' + wx.getStorageSync('openid');
    wxRequest.postRequest(monthUrl, data, this.monthUrlSuccess);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    this.setData({
      PageCur: currentPage.route
    })

    //获取当前页面的数据
    var userUrl = '/api/wx/user?wxToken=' + wx.getStorageSync('openid');
    wxRequest.getRequest(userUrl, this.dakaListSuccess);
  },

  dakaListSuccess: function (res) {
    console.log(res);
    var windowWidth = 320;
    try {
      var wxres = wx.getSystemInfoSync();
      windowWidth = wxres.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: res.data.data.pieChartData,
      width: windowWidth,
      height: 300,
      dataLabel: true,
    });

    this.setData({
      dakaArr: res.data.data.dakaArr,
      user: res.data.data.user,
      color: res.data.data.monthList,
      lastTime: res.data.data.lastTime,
      statrTime: res.data.data.statrTime,
      pieChartData: res.data.data.pieChartData,
      total: res.data.data.total,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})